package org.csu.code.api.controller;

import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Cart;
import org.csu.code.api.entity.Item;
import org.csu.code.api.entity.Product;
import org.csu.code.api.persistence.ItemMapper;
import org.csu.code.api.persistence.ProductMapper;
import org.csu.code.api.service.CartService;
import org.csu.code.api.service.CatalogService;
import org.csu.code.api.vo.CartItemVO;
import org.csu.code.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    Cart cart;
    @Autowired
    CatalogService catalogService;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductMapper productMapper;

    @GetMapping("/{userid}")
    @ResponseBody
    //查询用户的购物车
    public CommonResponse<List<CartItemVO>> viewCart(@PathVariable("userid") int userid){
        return cartService.getCartItemByUserid(userid);
    }

    @GetMapping("/{userid}/{itemid}/{quantity}/add")
    @ResponseBody
    //添加商品进入购物车
    public CommonResponse<CartVO> addItem(@PathVariable("userid") int userid, @PathVariable("itemid") int itemid, @PathVariable("quantity") int quantity){
        return cartService.addItem(userid, itemid, quantity);
    }

    @GetMapping("/{userid}/{itemid}/remove")
    @ResponseBody
    //删除购物车商品
    public CommonResponse<CartVO> removeItem(@PathVariable("userid") int userid,@PathVariable("itemid") int itemid){
        return cartService.deleteCartitemByUserid(userid,itemid);
    }

    @GetMapping("/{userid}/{itemid}/{quantity}/update")
    @ResponseBody
    //更新购物车
    public CommonResponse<BigDecimal> updateCart(@PathVariable("userid") int userid,@PathVariable("itemid") int itemid,@PathVariable("quantity") int quantity){
        return cartService.updateQuantity(userid,quantity,itemid);
    }

    @GetMapping("/{userid}/clear")
    @ResponseBody
    //清空购物车
    public CommonResponse<Cart> removeAll(@PathVariable("userid") int userid){
        return cartService.deleteByUserid(userid);
    }
}
